package com.cy.module;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.RoleLabel;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

@ControllerBind(controllerKey = "/clmp/roleLabel")
public class RoleLabelController extends WebApiController {

    @Override
    public void index() {
        RoleLabel roleLabel = getModel(RoleLabel.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Page<RoleLabel> roleLabelPage = RoleLabel.dao.paginate(page, pageSize, RoleLabel.SELECT_ALL, roleLabel);
        renderBuiPage(roleLabelPage, 200);
    }


    @Override
    public void update() {
        int id = getParaToInt();
        RoleLabel tempRoleLabel = getModel(RoleLabel.class);
        RoleLabel roleLabel = RoleLabel.dao.findById(id);
        roleLabel.cover(tempRoleLabel);

        if (roleLabel.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        RoleLabel roleLabel = getModel(RoleLabel.class);
        if(null == roleLabel.getCreateTime()){
            roleLabel.setCreateTime(new Date());
        }
        if (roleLabel.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            RoleLabel.dao.deleteById(id);
        }
        renderSuccess();
    }
}
